package com.dmgis.qxfw.dataservice.businessservice.yyrb.controller;

import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.base_product;
import com.dmgis.qxfw.dataservice.businessservice.common.util.DateTime;
import com.dmgis.qxfw.dataservice.businessservice.common.util.FileUtil;
import com.dmgis.qxfw.dataservice.businessservice.common.util.Word2PdfUtil;
import com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.dataservice.service.Impl.MongoService1;
import com.dmgis.qxfw.dataservice.businessservice.yyrb.service.IFarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/xtrb")
public class FarmController {
	private  final String operatingSystem;

	@Autowired
	public FarmController(String operatingSystem) {
		this.operatingSystem = operatingSystem;
	}
	@Resource
    IFarmService farmService;

	MongoService1 ms = new MongoService1();

	/**
	 * 初始化登录查找页面
	 * @return
	 */
	@RequestMapping("/initLogQueryPage")
	@ResponseBody
	public Object initLogQueryPage() {
		ArrayList<String> result = new ArrayList<String>();
		if(farmService.getLogNewDate()!=null){
			DateTime endDate = new DateTime(farmService.getLogNewDate());
			result.add(endDate.AddDays(-7).toDateString());
			result.add(endDate.toDateString());
		}
		return result;
	}

	/**
	 * 获取农业
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getLogInfo")
	@ResponseBody
	public Object getLogInfo(HttpServletRequest request, Model model) throws Exception {
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		String type = request.getParameter("type");
		List<base_product> result = farmService.getLogInfo(startDate,endDate,type);
		return result;
	}

	/**
	 *显示表格文档
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/showformDoc")
	@ResponseBody
	public String showDoc(HttpServletRequest request, Model model) {
		String servicePath = "";
		String id = request.getParameter("id");// 获取编号
		String fileName = request.getParameter("filename");
		String urlpath ;
		if (operatingSystem.equals("Windows")){
			 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\farm\\";// 临时文件目录
		}else {
			 urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/farm/";// 临时文件目录
		}


		FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
		FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存

		String doctype = fileName.split("\\.")[fileName.split("\\.").length - 1];// 文件类型
		if (doctype.contains("doc")) {
			String localPath = urlpath + fileName;
			String pdfPath = urlpath + fileName.replace(doctype, "pdf");
			File pdfFile = new File(pdfPath);
			if (pdfFile.exists()) {
				if (operatingSystem.equals("Windows")){
					servicePath = "Temp\\farm\\" + fileName.replace(doctype, "pdf");
				}else {
					servicePath = "Temp/farm/" + fileName.replace(doctype, "pdf");
				}

			} else {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("id", id);
				map.put("table_name", "base_product");
				map.put("collectionName", "doc_file");
				map.put("advCode", DbConfig.advCode);
				byte[] data = ms.selectData(map);
				FileUtil.bytesToFile(data, localPath);
				// 下载doc文件
				if (data == null) {
					return "";
				}
				File localFile = new File(localPath);// 本地文件
				if (localFile.exists()) {
					try {
						Word2PdfUtil.doc2pdf(localPath, pdfPath);
						if (operatingSystem.equals("Windows")){
							servicePath = "Temp\\farm\\" + fileName.replace(doctype, "pdf");
						}else {
							servicePath = "Temp/farm/" + fileName.replace(doctype, "pdf");
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		}
		servicePath = servicePath.replace("\\", "/");
		return servicePath;
	}
}

